import logging
import os
from logging.handlers import RotatingFileHandler

if not os.path.exists('log'):
    os.makedirs('log')

log_format = logging.Formatter(
    '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)

def setup_logger(name, log_file, level=logging.INFO):
    """设置日志记录器"""
    handler = RotatingFileHandler(
        f'log/{log_file}',
        maxBytes=10*1024*1024,  # 10MB
        backupCount=5,
        encoding='utf-8'
    )
    handler.setFormatter(log_format)

    logger = logging.getLogger(name)
    logger.setLevel(level)
    logger.addHandler(handler)

    return logger

main_logger = setup_logger("main", "main.log")

main_logger.info("启动")
